package com.jewaton.auth.commons.util;



/**
 * 
 * 文件名获取工具类
 */
public class FileNameUtil {

	
	public static String[] cutPath(String[] array){
		String[] newarray = new String[array.length];
		for (int i = 0; i < array.length; i++){
			newarray[i] = getNameWithExtension(array[i]);
		}
		return newarray;
	}
	
	/**
	 * 获取文件名，包括扩展名
	 * @param filename
	 * @return
	 */
	public static String getNameWithExtension(String filename){
		String newName = null;
		if ((filename != null) && (filename.length() > 0)) {
			if (filename.contains("/")){
				newName = filename.substring(filename.lastIndexOf("/")+1);
			}
			if (filename.contains("\\")){
				newName = filename.substring(filename.lastIndexOf("\\")+1);
			}
		}
		return newName;
	}
	
	/**
	 * 获取文件的名字（不含扩展名）
	 * 
	 * @param filename
	 *            文件完整名
	 * @return
	 */
	public static String getName(String filename) {
		if ((filename != null) && (filename.length() > 0)) {
			if (filename.contains("/")){
				filename = filename.substring(filename.lastIndexOf("/")+1);
			}
			if (filename.contains("\\")){
				filename = filename.substring(filename.lastIndexOf("\\")+1);
			}
			int dot = filename.lastIndexOf('.');
			if ((dot > -1) && (dot < (filename.length() - 1))) {
				return filename.substring(0, dot);
			}
		}
		return filename;
	}

	public static String getParentPath(String path){
		if (path.contains("\\")){
			if (path.contains("/")){
				path = path.replace("/", "\\");
				path = path.subSequence(0, path.lastIndexOf("\\")).toString();
			}
		}
		if (path.contains("/")){
			if (path.contains("\\")){
				path = path.replace("\\", "/");
				path = path.subSequence(0, path.lastIndexOf("/")).toString();
			}
		}
		return path;
	}
	
	/**
	 * 获取文件的扩展名
	 * 
	 * @param filename
	 *            文件完整文字
	 * @return
	 */
	public static String getExtensionName(String filename) {
		if ((filename != null) && (filename.length() > 0)) {
			int dot = filename.lastIndexOf('.');
			if ((dot > -1) && (dot < (filename.length() - 1))) {
				return filename.substring(dot + 1);
			}
		}
		return filename;
	}
	
}
